define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'virtualmachineapply/index' + location.search,
                    add_url: 'virtualmachineapply/add',
                    edit_url: 'virtualmachineapply/edit',
                    del_url: 'virtualmachineapply/del',
                    multi_url: 'virtualmachineapply/multi',
                    import_url: 'virtualmachineapply/import',
                    table: 'virtualmachine_apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'status desc,passingtime desc',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'admin_id', title: __('Admin_id'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'admin_name', title: __('Admin_id'), operate: false},
                        // {field: 'manual', title: __('手动申请人'), operate: 'LIKE'},
                        {field: 'num', title: __('申请数量'), operate: 'LIKE'},
                        {field: 'numbertype', title: __('区域'), operate: 'LIKE'},
                        {field: 'payment_method', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'自动支付': 'info','手动支付':'warning'}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'passingtime', title: __('通过审核时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('申请通过'),"hidden":__('申请中'),"reassign":__('重新分配'),"refuse":__('拒绝'),"manual":__('手动申请通过'),"part":__('分配部分账户')}, formatter: Table.api.formatter.status,custom: {'Normal':'success', 'hidden':'danger', 'reassign':'info', 'part':'分配部分账户', 'refuse':'gray', 'manual':'success'}},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('快捷自动处理'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('一键通过'),
                                    title: __('一键通过'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '确认自动分配账户吗？',
                                    url: 'virtualmachineapply/through',
                                    visible:function (row) {
                                        if(row.status == 'hidden'){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#znum").text(data.Statistics.znum);
                $("#ztoday").text(data.Statistics.ztoday);
                $("#nohandle").text(data.Statistics.nohandle);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-virtualmachine").data("formatItem",function (item) {
                return "账户编号:"+item.number+","+item.numbername_email;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-virtualmachine").data("formatItem",function (item) {
                return "账户编号:"+item.number+","+item.numbername_email;
            });
            // $("#c-virtualmachine").data("params", function (obj) {
            //     return {custom: {type_operate: 0,admin_status: 'normal',payment_method: $("#cc_payment_method").val(),numbertype: $("#cc_numbertype").val()}};
            // });
            Controller.api.bindevent();
        },
        manual: function () {
            $("#c-virtualmachine").data("formatItem",function (item) {
                return "账户编号:"+item.number+","+item.numbername_email;
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});